package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.ProductProductTagDTO;
import com.wgh.product.entity.shop.ProductProductTag;
import com.wgh.product.service.ProductProductTagService;
import com.wgh.product.converter.ProductProductTagConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品标签关联控制器
 * 提供商品标签关联相关的RESTful API
 */
@RestController
@RequestMapping("/api/product-product-tag")
public class ProductProductTagController {

    @Autowired
    private ProductProductTagService productProductTagService;
    
    @Autowired
    private ProductProductTagConverter productProductTagConverter;
    
    /**
     * 根据商品ID查询关联的标签ID列表
     */
    @GetMapping("/product/{productId}")
    public Result<List<Long>> getTagIdsByProductId(@PathVariable Long productId) {
        try {
            List<Long> tagIds = productProductTagService.getTagIdsByProductId(productId);
            return Result.success(tagIds);
        } catch (Exception e) {
            return Result.error("获取商品关联标签ID列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据标签ID查询关联的商品ID列表
     */
    @GetMapping("/tag/{tagId}")
    public Result<List<Long>> getProductIdsByTagId(@PathVariable Long tagId) {
        try {
            List<Long> productIds = productProductTagService.getProductIdsByTagId(tagId);
            return Result.success(productIds);
        } catch (Exception e) {
            return Result.error("获取标签关联商品ID列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量添加商品标签关联
     */
    @PostMapping("/batch")
    public Result<Boolean> addBatch(@RequestBody List<ProductProductTagDTO> productProductTagDTOs) {
        try {
            List<ProductProductTag> productProductTags = productProductTagConverter.toEntityList(productProductTagDTOs);
            boolean result = productProductTagService.addBatch(productProductTags);
            return result ? Result.success(true) : Result.error("批量添加商品标签关联失败");
        } catch (Exception e) {
            return Result.error("批量添加商品标签关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据商品ID删除商品标签关联
     */
    @DeleteMapping("/product/{productId}")
    public Result<Boolean> deleteByProductId(@PathVariable Long productId) {
        try {
            boolean result = productProductTagService.deleteByProductId(productId);
            return result ? Result.success(true) : Result.error("删除商品标签关联失败");
        } catch (Exception e) {
            return Result.error("删除商品标签关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据标签ID删除商品标签关联
     */
    @DeleteMapping("/tag/{tagId}")
    public Result<Boolean> deleteByTagId(@PathVariable Long tagId) {
        try {
            boolean result = productProductTagService.deleteByTagId(tagId);
            return result ? Result.success(true) : Result.error("删除标签关联的商品失败");
        } catch (Exception e) {
            return Result.error("删除标签关联的商品失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据商品ID和标签ID列表删除关联
     */
    @DeleteMapping("/product-tag")
    public Result<Boolean> deleteByProductIdAndTagIds(
            @RequestParam Long productId,
            @RequestParam List<Long> tagIds) {
        try {
            boolean result = productProductTagService.deleteByProductIdAndTagIds(productId, tagIds);
            return result ? Result.success(true) : Result.error("删除指定的商品标签关联失败");
        } catch (Exception e) {
            return Result.error("删除指定的商品标签关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 添加单条商品标签关联
     */
    @PostMapping
    public Result<Boolean> addProductProductTag(@RequestBody ProductProductTagDTO productProductTagDTO) {
        try {
            ProductProductTag productProductTag = productProductTagConverter.toEntity(productProductTagDTO);
            boolean result = productProductTagService.save(productProductTag);
            return result ? Result.success(true) : Result.error("添加商品标签关联失败");
        } catch (Exception e) {
            return Result.error("添加商品标签关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除商品标签关联
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProductProductTag(@PathVariable Long id) {
        try {
            boolean result = productProductTagService.removeById(id);
            return result ? Result.success(true) : Result.error("删除商品标签关联失败");
        } catch (Exception e) {
            return Result.error("删除商品标签关联失败：" + e.getMessage());
        }
    }
}